module.exports = (plop) => {
  plop.setGenerator('package', {
    description: 'create a package',
    prompts: [
      // 名称
      {
        type: 'input',
        name: 'name',
        message: 'package name',
        default: 'package',
      },
      // cdn 打包
      {
        type: 'input',
        name: 'cdn',
        message: 'cdn?',
        default: 'y',
      },
    ],
    actions(prompts) {
      return answerYes(prompts.cdn)
        ? [
            {
              type: 'add',
              path: 'src/az-{{kebabCase name}}/index.js',
              templateFile: 'template/package/index.hbs',
            },
            {
              type: 'add',
              path: 'src/az-{{kebabCase name}}/cdn.js',
              templateFile: 'template/package/cdn.hbs',
            },
            {
              type: 'add',
              path: 'src/az-{{kebabCase name}}/build.js',
              templateFile: 'template/package/buildCdn.hbs',
            },
            {
              type: 'add',
              path: 'test/az-{{kebabCase name}}/index.test.js',
              templateFile: 'template/package/test.hbs',
            },
            {
              type: 'add',
              path: 'packages/az-{{kebabCase name}}/LICENSE',
              templateFile: 'template/package/LICENSE',
            },
            {
              type: 'add',
              path: 'packages/az-{{kebabCase name}}/package.json',
              templateFile: 'template/package/packageCdn.hbs',
            },
            {
              type: 'add',
              path: 'packages/az-{{kebabCase name}}/README.md',
              templateFile: 'template/package/README.hbs',
            },
          ]
        : [
            {
              type: 'add',
              path: 'src/az-{{kebabCase name}}/index.js',
              templateFile: 'template/package/index.hbs',
            },
            {
              type: 'add',
              path: 'src/az-{{kebabCase name}}/build.js',
              templateFile: 'template/package/build.hbs',
            },
            {
              type: 'add',
              path: 'packages/az-{{kebabCase name}}/LICENSE',
              templateFile: 'template/package/LICENSE',
            },
            {
              type: 'add',
              path: 'packages/az-{{kebabCase name}}/package.json',
              templateFile: 'template/package/package.hbs',
            },
            {
              type: 'add',
              path: 'packages/az-{{kebabCase name}}/README.md',
              templateFile: 'template/package/README.hbs',
            },
          ]
    },
  })
}

function answerYes(answer) {
  return ['y', 'ye', 'ys', 'yes'].includes(answer.toLowerCase())
}
